package com.dhx.web;

import com.dhx.dao.CompanyInfoDao;
import com.dhx.dao.CompanyNeedDao;
import com.dhx.dao.TongjiDao;
import com.dhx.dao.impl.CompanyInfoDaoImpl;
import com.dhx.dao.impl.CompanyNeedImpl;
import com.dhx.dao.impl.TongjiDaoImpl;
import com.dhx.pojo.CompanyInfo;
import com.dhx.pojo.CompanyNeed;
import com.dhx.pojo.LeaveStudent;
import com.dhx.pojo.Tongji;
import com.dhx.service.InfoService;
import com.dhx.service.InfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class InfoServlet extends HttpServlet {
    private InfoService infoService = new InfoServiceImpl();
    private CompanyInfoDao companyInfoDao = new CompanyInfoDaoImpl();
    private CompanyNeedDao companyNeedDao = new CompanyNeedImpl();
    private TongjiDao tongjiDao = new TongjiDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String action = req.getParameter("action");
        if ("companyinfo".equals(action)) {
            List<CompanyInfo> infos = companyInfoDao.queryInfoes();
            req.setAttribute("compinfo", infos);
            req.getRequestDispatcher("/pages/userview/usercompanyinfo.jsp").forward(req, resp);
        } else if ("leavestu".equals(action)) {
            List<LeaveStudent> infos = infoService.queryInfoes();
            req.setAttribute("info", infos);
            req.getRequestDispatcher("/pages/userview/userleavestu.jsp").forward(req, resp);
        } else if ("companyneed".equals(action)) {
            List<CompanyNeed> infos = companyNeedDao.queryInfoes();
            req.setAttribute("companyneed", infos);
            req.getRequestDispatcher("/pages/userview/usercompanyneed.jsp").forward(req, resp);
        } else if ("tongji".equals(action)) {
            List<Tongji> infos = tongjiDao.queryInfoes();
            req.setAttribute("tongji", infos);
            req.getRequestDispatcher("/pages/userview/usertongji.jsp").forward(req, resp);
        } else if ("mleavestu".equals(action)) {
            List<LeaveStudent> infos = infoService.queryInfoes();
            req.setAttribute("info", infos);
            req.getRequestDispatcher("/pages/manager/managerleavestu.jsp").forward(req, resp);
        } else if ("mcompanyneed".equals(action)) {
            List<CompanyNeed> infos = companyNeedDao.queryInfoes();
            req.setAttribute("companyneed", infos);
            req.getRequestDispatcher("/pages/manager/managercompanyneed.jsp").forward(req, resp);
        } else if ("mtongji".equals(action)) {
            List<Tongji> infos = tongjiDao.queryInfoes();
            req.setAttribute("tongji", infos);
            req.getRequestDispatcher("/pages/manager/managertongji.jsp").forward(req, resp);
        } else if ("mcompanyinfo".equals(action)) {
            List<CompanyInfo> infos = companyInfoDao.queryInfoes();
            req.setAttribute("compinfo", infos);
            req.getRequestDispatcher("/pages/manager/managercompanyinfo.jsp").forward(req, resp);
        } else if ("addcompanyinfo".equals(action)) {
            String name = req.getParameter("name");
            String cno = req.getParameter("cno");
            String area = req.getParameter("area");
            String ano = req.getParameter("ano");
            String dengji = req.getParameter("dengji");
            companyInfoDao.addInfo(new CompanyInfo(name, cno, area, ano, dengji));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mcompanyinfo");
        } else if ("delcompanyinfo".equals(action)) {
            String no = req.getParameter("cno");
            companyInfoDao.deleteInfoById(no);
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mcompanyinfo");
        } else if ("updatecompanyinfo".equals(action)) {
            String name = req.getParameter("name");
            String cno = req.getParameter("cno");
            String area = req.getParameter("area");
            String ano = req.getParameter("ano");
            String dengji = req.getParameter("dengji");
            companyInfoDao.updateInfo(new CompanyInfo(name, cno, area, ano, dengji));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mcompanyinfo");
        } else if ("addcompanyneed".equals(action)) {
            String companyarea = req.getParameter("companyarea");
            String cno = req.getParameter("cno");
            String major = req.getParameter("major");
            String neednum = req.getParameter("neednum");
            String date = req.getParameter("date");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date datee = null;
            int numm = 0;
            try {
                datee = df.parse(date);
                numm = Integer.parseInt(neednum);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            companyNeedDao.addInfo(new CompanyNeed(cno, major, numm, date));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mcompanyneed");
        } else if ("delcompanyneed".equals(action)) {
            String no = req.getParameter("cno");
            companyNeedDao.deleteInfoById(no);
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mcompanyneed");
        } else if ("updatecompanyneed".equals(action)) {
            String companyarea = req.getParameter("companyarea");
            String cno = req.getParameter("cno");
            String major = req.getParameter("major");
            String neednum = req.getParameter("neednum");
            String date = req.getParameter("date");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date datee = null;
            int numm = 0;
            try {
                datee = df.parse(date);
                numm = Integer.parseInt(neednum);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            companyNeedDao.updateInfo(new CompanyNeed(cno, major, numm, date));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mcompanyneed");
        } else if ("addtongji".equals(action)) {
            String date = req.getParameter("date");
            String cno = req.getParameter("cno");
            String major = req.getParameter("major");
            String area = req.getParameter("area");
            tongjiDao.addInfo(new Tongji(date, cno, major, area));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mtongji");
        } else if ("deltongji".equals(action)) {
            String no = req.getParameter("cno");
            System.out.println(no);
            tongjiDao.deleteInfoById(no);
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mtongji");
        } else if ("updatetongji".equals(action)) {
            String date = req.getParameter("date");
            String cno = req.getParameter("cno");
            String major = req.getParameter("major");
            String area = req.getParameter("area");
            Tongji tt = new Tongji(date, cno, major, area);
            System.out.println(tt);
            tongjiDao.updateInfo(new Tongji(date, cno, major, area));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mtongji");
        } else if ("addleavestu".equals(action)) {
            String date = req.getParameter("date");
            String no = req.getParameter("no");
            String major = req.getParameter("major");
            String school = req.getParameter("school");
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String level = req.getParameter("level");
            String age = req.getParameter("age");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date datee = null;
            int agee = 0;
            try {
                agee = Integer.parseInt(age);
                datee = df.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            infoService.addInfo(new LeaveStudent(name, no, major, sex, level, agee, datee, school));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mleavestu");
        } else if ("delleavestu".equals(action)) {
            String no = req.getParameter("cno");
            infoService.deleteInfoById(no);
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mleavestu");
        } else if ("updateleavestu".equals(action)) {
            String date = req.getParameter("date");
            String no = req.getParameter("no");
            String major = req.getParameter("major");
            String school = req.getParameter("school");
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String level = req.getParameter("level");
            String age = req.getParameter("age");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date datee = null;
            int agee = 0;
            try {
                agee = Integer.parseInt(age);
                datee = df.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            infoService.updateInfo(new LeaveStudent(name, no, major, sex, level, agee, datee, school));
            resp.sendRedirect(req.getContextPath() + "/infoservlet?action=mleavestu");
        } else if ("getstu".equals(action)) {
            String no = req.getParameter("cno");
            LeaveStudent leaveStudent = infoService.queryInfoById(no);
            req.setAttribute("name", leaveStudent.getName());
            req.setAttribute("no", leaveStudent.getNo());
            req.setAttribute("major", leaveStudent.getMajor());
            req.setAttribute("sex", leaveStudent.getSex());
            req.setAttribute("level", leaveStudent.getLevel());
            req.setAttribute("sex", leaveStudent.getSex());
            req.setAttribute("age", leaveStudent.getAge());
            req.setAttribute("date", leaveStudent.getDate());
            req.setAttribute("school", leaveStudent.getSchool());
            req.getRequestDispatcher("/pages/manager/update/updateUser.jsp").forward(req, resp);
        } else if ("getcomp".equals(action)) {
            String no = req.getParameter("cno");
            CompanyInfo companyInfo = companyInfoDao.queryInfoById(no);
            req.setAttribute("name", companyInfo.getName());
            req.setAttribute("no", companyInfo.getCno());
            req.setAttribute("major", companyInfo.getArea());
            req.setAttribute("sex", companyInfo.getAno());
            req.setAttribute("level", companyInfo.getDengji());
            req.getRequestDispatcher("/pages/manager/update/updateQiyeXuqiu.jsp").forward(req, resp);
        } else if ("getcompneed".equals(action)) {
            String no = req.getParameter("cno");
            CompanyNeed companyNeed = companyNeedDao.queryInfoById(no);
            req.setAttribute("cno", companyNeed.getCno());
            req.setAttribute("major", companyNeed.getMajor());
            req.setAttribute("need", companyNeed.getNeednum());
            req.setAttribute("date", companyNeed.getDate());
            req.getRequestDispatcher("/pages/manager/update/updateQiye.jsp").forward(req, resp);
        } else if ("gettongji".equals(action)) {
            String no = req.getParameter("cno");
            Tongji tongji = tongjiDao.queryInfoById(no);
            req.setAttribute("date", tongji.getDate());
            req.setAttribute("major", tongji.getMajor());
            req.setAttribute("cno", tongji.getCno());
            req.setAttribute("area", tongji.getArea());
            req.getRequestDispatcher("/pages/manager/update/updateXinxiTongji.jsp").forward(req, resp);
        } else if ("mbeifen".equals(action)) {
            resp.sendRedirect("/Student_Work/pages/manager/managerbeifen.jsp");
            // req.getRequestDispatcher("/pages/manager/managerbeifen.jsp").forward(req, resp);
        }

    }
}
